#!/usr/bin/python
# -*- coding: UTF-8 -*-
from  datetime  import  * 
import pymysql, sys, urllib.request, json, time, uuid

m_host = 'rm-2ze2us2r61990270t.mysql.rds.aliyuncs.com'
m_user = 'shsytour'
m_passwd = '123456'
m_db = 'shsytour'
m_charset = 'utf8'

# m_host = 'localhost'
# m_user = 'root'
# m_passwd = 'root'
# m_db = 'test'
# m_charset = 'utf8'

conn = pymysql.connect(host=m_host,user=m_user,passwd=m_passwd,db=m_db,charset=m_charset);
cur = conn.cursor()
now = datetime.now()
def createCalendar(id):
	ssql = "SELECT datetimes FROM user_caravan_calendar WHERE caravan_id = %s ORDER BY datetimes DESC LIMIT 1"
	cur.execute(ssql, (id))
	stu = cur.fetchone()
	c = 366
	if stu:
		c = ((now + timedelta(days = 365)).date() - stu[0]).days
	if c > 0:
		isql = 'INSERT INTO user_caravan_calendar (caravan_id,datetimes,created_at,updated_at) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)'
		created_at = str(datetime.fromtimestamp(time.time()))
		caravan_id = id	
		for x in range(c):
			datetimes = str((now + timedelta(days = x)).date())
			print(caravan_id, datetimes)
			cur.execute(isql, [caravan_id,datetimes,created_at,created_at])
		conn.commit()


def getCaravans():
	cur.execute("select * from caravans_cn")
	stus = cur.fetchall()
	if stus:
		for stua in stus:
			createCalendar(stua[0])
	else:
		print('data is empty!')
	cur.close()
	conn.close()

def updateCaravansStatus():
	cur.execute("select *,TIMESTAMPDIFF(MINUTE,send_time,now()) as diff from user_messages")
	cur.close()
	conn.close()
getCaravans()